#pragma region Copyright
/// <copyright>
/// Copyright (c) 2007 Ilia Shramko.
/// Released under the terms of the GNU Lesser General Public License.
/// </copyright>
#pragma endregion

#pragma once

class OSHSource;

class __declspec(uuid("861EF3F1-8EC0-4BD8-A469-00BB82F0A8D8")) OSHBitmap;

class ATL_NO_VTABLE OSHBitmap : 
	public CComObjectRootEx<CComMultiThreadModel>,
	public CComCoClass<OSHBitmap, &__uuidof(OSHBitmap)>,
	public IDispatchImpl<OSHLibrary::IBitmap, &__uuidof(OSHLibrary::IBitmap), &__uuidof(OSHLibrary::__OSH), /*wMajor =*/ -1, /*wMinor =*/ -1>
{
private:
	CComPtr<IUnknown> _unkMarshaler;
	OSHSource* _oshSource;
	int _bitmapNumber;
	SYSTEMTIME _refTime;
	BITMAPINFOHEADER* _bitmapInfo;

	bool IsOpened() const;

public:
	OSHBitmap();
	virtual ~OSHBitmap();

	//DECLARE_REGISTRY_RESOURCEID(IDR_OSHBITMAP)
	//DECLARE_NO_REGISTRY()
	DECLARE_GET_CONTROLLING_UNKNOWN()

	BEGIN_COM_MAP(OSHBitmap)
		COM_INTERFACE_ENTRY(OSHLibrary::IBitmap)
		COM_INTERFACE_ENTRY(IDispatch)
		COM_INTERFACE_ENTRY_AGGREGATE(IID_IMarshal, _unkMarshaler.p)
	END_COM_MAP()

	DECLARE_PROTECT_FINAL_CONSTRUCT()

	HRESULT FinalConstruct()
	{
		return ::CoCreateFreeThreadedMarshaler(GetControllingUnknown(), &_unkMarshaler.p);
	}
	
	void FinalRelease() 
	{
		_unkMarshaler.Release();
	}

public:

	HRESULT Initialize(OSHSource* source, int bitmapNumber, void* bitmapPtr, SYSTEMTIME* time);
	int GetBitmapNumber() { return _bitmapNumber; }
	SYSTEMTIME GetCreated() { return _refTime; }

	// IBitmap Members
	STDMETHOD(get_Created)(DATE* created);
	STDMETHOD(get_BitmapInfo)(LONG* bitmapInfo);
	STDMETHOD(Save)(BSTR filePath);
	STDMETHOD(Close)();

};

//OBJECT_ENTRY_AUTO(__uuidof(OSHBitmap), OSHBitmap)
